package com.yuzhihao.writejvm.clazz;

import com.yuzhihao.writejvm.format.ClassFormatEnum;
import com.yuzhihao.writejvm.format.PrintFormat;
import com.yuzhihao.writejvm.read.Parse;
import lombok.Data;
import lombok.extern.log4j.Log4j2;

import java.io.DataInputStream;
import java.io.IOException;

/**
 * 魔法值
 *
 * @author yzh
 * @date 2021年11月29日18:32:13
 */
@Data
@Log4j2
public class Magic implements Parse {

    private ClassFormatEnum cfe = ClassFormatEnum.magic;

    private String magic;


    @Override
    public void parse(DataInputStream dis, ClassInfo ci) {
        byte[] bytes = new byte[cfe.getB()];
        try {
            dis.readFully(bytes);

            this.magic = Parse.binaryToHexString(bytes);

        } catch (IOException e) {
            log.error(e.getMessage());
        }

        this.out(ci);
    }

    public void out(ClassInfo ci) {
        ci.setMagic(this);
    }

    @Override
    public String toString() {
        return PrintFormat.EMPTY + "magic " + this.magic + System.lineSeparator();
    }
}
